//======================================================================================
//  Notes:Use this command to anonymously check out the latest project source code:
//  Non-members may check out a read-only working copy anonymously over HTTP.
//  http://YHQCodingWsp.googlecode.com/svn/trunk/
//
//  Author:		Kevin Yang
//  Email:		Bigearrabbit@qq.com
//
//  DATE:		2013-4-16
//
//  File Name:	YHQSvnFolderAdd.cpp
//
//  Notes:
//
//======================================================================================
#include "YHQSvnFolderAdd.h"

//YHQCodingCore Library
#include "YHQFile.h"

//Qt Library
#include <QDir>
#include <QStringList>
#include <QProcess>


//头文件包含
#include <QFileInfo>
#include <QString>
#include <QList>
#include <QFile>


#include <iostream>

//LocalOutPutMessage
#define LocalOutPutMessage(iString) \
    if(m_pOutsideTextEdit)m_pOutsideTextEdit->append(iString)

#define LocalRunSvnline(iString) \
    pMyProcess->start(iString);\
    pMyProcess->waitForFinished(100);\
    LocalOutPutMessage(iString)

//----------------------------------------------------
YHQSvnFolderAdd::YHQSvnFolderAdd()
    : m_pOutsideTextEdit(NULL)
    , m_ListOfIgnore()
{
}
//----------------------------------------------------
bool YHQSvnFolderAdd::SvnInitialAddSingle(const QString & iFolderDir,
                                          QString & iotextForAdd)
{
    iotextForAdd.clear();//clear
    QDir dir(iFolderDir);//instance a dir
    if (!dir.exists())//check exist
    {
        LocalOutPutMessage("folder is not exist:" + iFolderDir );
        return false;// not exist
    }

    //check if exist .svn
    QDir dirsvn(dir.absoluteFilePath(".svn"));
    if (dirsvn.exists())//check exist
    {
        LocalOutPutMessage(" !!!Warning: Need not add to svn: because it is svn top folder" + iFolderDir );
        return false;// not exist
    }

    //loop sub folder for first Level
    iotextForAdd = QString("svn add \"%1\" --depth empty").arg(iFolderDir);
    return true;
}
//----------------------------------------------------
bool YHQSvnFolderAdd::SvnInitialAddTop(const QString & iFolderDir)
{
    QDir dir(iFolderDir);//instance a dir
    if (!dir.exists())//check exist
    {
        LocalOutPutMessage("folder is not exist:" + iFolderDir );
        return false;// not exist
    }
    QStringList list = dir.entryList(QDir::AllDirs|
                                     QDir::NoDotAndDotDot|
                                     QDir::System|
                                     QDir::Hidden);//List of dirs

    //find .svn first
    const int size = list.size();//sub size
    bool isSVNFolder = false;//not svn
    for (int i=0;i<size;i++)
    {
        QString fileInfo = list.at(i).toLower();// to lower

        //if in list
        if(fileInfo == ".svn")
        {
            isSVNFolder = true;
            break;
        }//if
    }//for i

    //not svn
    if(!isSVNFolder)
    {
        LocalOutPutMessage("Error: input folder is not Under svn." );
        return false;// not exist
    }
    return SvnInitialAdd(iFolderDir);
}
//----------------------------------------------------
bool YHQSvnFolderAdd::SvnInitialAdd(const QString & iFolderDir)
{
    QDir dir(iFolderDir);//instance a dir
    if (!dir.exists())//check exist
    {
        LocalOutPutMessage("folder is not exist:" + iFolderDir );
        return false;// not exist
    }

    //pre defination
    QString textLine;
    //debug:
    QProcess * pMyProcess = new QProcess();//new one
    bool re = true;

    QStringList IgnoreList;//ignore list

    {
        QStringList list = dir.entryList(QDir::AllDirs|
                                         QDir::NoDotAndDotDot|
                                         QDir::System|
                                         QDir::Hidden);//List of dirs

        //find .svn first
        const int size = list.size();//sub size
        //loop sub folder for first Level
        for (int i=0;i<size;i++)
        {
            QString name = list.at(i);
            QString SubPath = dir.absoluteFilePath(name);
            QString fileInfoLow = name.toLower();// to lower

            //if in ignore list
            for(int j=0;j<m_ListOfIgnore.size();j++)
            {
                if(fileInfoLow == m_ListOfIgnore[j])
                {
                    IgnoreList.append(name);//append to list
                    goto labelEndI;

                }//if
            }
            re = SvnInitialAddSingle(SubPath,textLine);
            if(re)
            {
                LocalRunSvnline(textLine);//LocalRunSvnline

                SvnInitialAdd(SubPath);//call self
            }

labelEndI:;

        }//for i
    }

    //loop File
    {

        QStringList list = dir.entryList(QDir::Files|
                                         QDir::System|
                                         QDir::Hidden);//List of Files

        //find .svn first
        const int size = list.size();//sub size
        //loop sub folder for first Level
        for (int i=0;i<size;i++)
        {
            QString name = list.at(i);
            QString SubPath = dir.absoluteFilePath(name);
            //QString fileInfoLow = name.toLower();// to lower

            //if in ignore list

            //loop sub folder for first Level
            textLine = QString("svn add \"%1\"").arg(SubPath);

            LocalRunSvnline(textLine);//LocalRunSvnline
labelEndI2:;

        }//for i
    }
    //add ignore list:
    switch (IgnoreList.size())
    {
    case 0:
        break;
    case 1:
    {
        textLine = QString("svn propset svn:ignore \"\%1\" \"\%2\"")
                .arg(IgnoreList.front()).arg(iFolderDir);
        LocalRunSvnline(textLine);//LocalRunSvnline
        break;
    }
    default:
    {
        //create Ignore file FileIgnore
        QString ignoreTempPath = dir.absoluteFilePath("yhhTempsvnIgnoreFile.temp");
        re = YHQFile::WriteToTextFile(ignoreTempPath,IgnoreList);
        if(re)
        {
            textLine = QString("svn propset svn:ignore -F \"\%1\" \"\%2\"")
                    .arg(ignoreTempPath).arg(iFolderDir);
            LocalRunSvnline(textLine);//LocalRunSvnline
            dir.remove(ignoreTempPath);//remove this file
        }
        break;
    }//default
    }//switch



    delete pMyProcess;pMyProcess = NULL;//delete

    return true;
}
//---------------------------------------------------------------------
